package com.yylending.las.personal.version;

import com.yyfax.infrastructure.base.BaseApplication;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IEvent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.Global;
import com.yylending.las.personal.Version;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by HenryZhang on 2016/11/23.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class VersionPresenter extends Presenter<VersionModel, VersionPresenter.View> {
	private final static String TAG = "VersionPresenter";

	private static final String SUFFIX_VERSION_NAME = "V ";
	private static final String POSTFIX_VERSION_NAME = "新版本更新:";
	private static final String NEED_FORCE_UPDATE = "1"; // 需强制更新
	private static final String DO_NOT_NEED_FORCE_UPDATE = "0"; // 无需强制更新
	private static final String POSTFIX_APK_NAME = "xxx";

	public static class Generator implements PresenterFactory<VersionPresenter> {
		@Override
		public VersionPresenter create() {
			return new VersionPresenter();
		}
	}

	public interface View extends IView {
		void hasNewVersion(boolean forceUpdate, String url, String content, String title);

		void isNewestVersion();
	}

	public static class Event extends IEvent {
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new VersionModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void checkVersion(boolean autoTips) {
		String params = BaseApplication.formatString(Global.VERSION_PARAMS, BaseApplication.versionCode);
		//Updater.checkVersion(aty, Global.VERSION_URL, params, BuildConfig.APPLICATION_ID);

//		VersionModel.getVersion(aty, new OnNextListener<BaseResultEntity<Version>>() {
//			@Override
//			public void onNext(BaseResultEntity<Version> result) {
//				if (YJKD.isResponseSucc(result) && null != result.getContent()) {
//					Version rs = result.getContent();
//					String local = YJKD.versionName.substring(1).replace(".", "");
//					String server = rs.version.substring(1).replace(".", "");
//					int code = Integer.parseInt(local);
//					int target = Integer.parseInt(server);
//					CLog.i(TAG, "local=" + local + ", server=" + server + ", code=" + code + ", target=" + target);
//					if (code < target) {
//						//show dialog
//					}
//				}
//			}
//		});
		execute(new VersionModel.Update(), FUNCTIONAL_CODE, autoTips, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void updateResult(VersionModel.UpdateResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result) && null != result.getData()) {
				Version bean = result.getData();
				String configVersion = bean.configVersion;
				if (!"2".equals(configVersion) || !Global.SOURCE.equals(bean.type)) {//判断配置版本信息和平台
					mView.isNewestVersion();
					return;
				}
				boolean enableAll = "1".equals(bean.enableAll);
				List<String> channelList = bean.enableChannels;
				if (!enableAll && !channelList.contains(BaseApplication.channel)) {
					mView.isNewestVersion();
					return;
				}
				Long remoteVersionCode = Long.valueOf(bean.versionCode);
				if (remoteVersionCode > BaseApplication.versionCode) {
					String url = bean.downloadUrl;
					String status = bean.forceUpdate;
					String remoteVersion = bean.versionName;

					boolean forceUpdate = false;
					String title = SUFFIX_VERSION_NAME + remoteVersion + POSTFIX_VERSION_NAME;
					if (NEED_FORCE_UPDATE.equals(status)) {
						forceUpdate = true;
					}else if (DO_NOT_NEED_FORCE_UPDATE.equals(status)) {
						forceUpdate = false;
					}

					mView.hasNewVersion(forceUpdate, url, bean.versionDesc, title);
				} else {
					mView.isNewestVersion();
				}
			} else {
				mView.isNewestVersion();
			}
		}
	}
}
